package com.yanqu.road.dao.impl.guess;

import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.guess.CommonGuessRewardItem;
import com.yanqu.road.entity.guess.data.CommonGuessMain;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CommonGuessMainDaoImpl extends TempDataObjectDao<CommonGuessMain> {

    public Map<Integer, Map<Integer, Map<Integer, Map<Integer, CommonGuessMain>>>> getData(String activityIds) {
        String selectSql = "select * from t_c_activity_common_guess_main where activityId in (" + activityIds + ")";
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            Map<Integer, Map<Integer, Map<Integer, Map<Integer, CommonGuessMain>>>> map = new ConcurrentHashMap<>();
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(selectSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CommonGuessMain data = parse(rs);
                Map<Integer, Map<Integer, Map<Integer, CommonGuessMain>>> groupMap = map.computeIfAbsent(data.getActivityId(), k -> new ConcurrentHashMap<>());
                Map<Integer, Map<Integer, CommonGuessMain>> roundMap = groupMap.computeIfAbsent(data.getGroupId(), k -> new ConcurrentHashMap<>());
                Map<Integer, CommonGuessMain> siteMap = roundMap.computeIfAbsent(data.getRound(), k -> new ConcurrentHashMap<>());
                siteMap.put(data.getSiteId(), data);
            }
            return map;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String insertSql = "INSERT INTO `t_c_activity_common_guess_main`(`activityId`, `groupId`, `round`, `siteId`, `type`, `playType`, `guessNum`, `startTime`, `endTime`, `endWaitResultTime`, `endShowTime`, `rankResult`, `reward`, `createTime`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    @Override
    protected boolean addData(CommonGuessMain data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSiteId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlayType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuessNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndWaitResultTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndShowTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRankResult()));
        String rewardStr = "";
        if (!data.getReward().isEmpty()) {
            rewardStr = JSONObject.toJSONString(data.getReward());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, rewardStr));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        if (getDb().executeNoneQuery(insertSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "UPDATE `t_c_activity_common_guess_main` SET `type` = ?, `playType` = ?, `guessNum` = ?, `startTime` = ?, `endTime` = ?, `endWaitResultTime` = ?, `endShowTime` = ?, `rankResult` = ?, `reward` = ? WHERE `activityId` = ? and `groupId` = ? AND `round` = ? AND `siteId` = ?";
    @Override
    protected boolean updateData(CommonGuessMain data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlayType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGuessNum()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getStartTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndWaitResultTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEndShowTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRankResult()));
        String rewardStr = "";
        if (!data.getReward().isEmpty()) {
            rewardStr = JSONObject.toJSONString(data.getReward());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, rewardStr));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getRound()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getSiteId()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CommonGuessMain parseData(ResultSet rs) throws SQLException {
        CommonGuessMain data = new CommonGuessMain();
        data.setActivityId(rs.getInt("activityId"));
        data.setGroupId(rs.getInt("groupId"));
        data.setRound(rs.getInt("round"));
        data.setSiteId(rs.getInt("siteId"));
        data.setType(rs.getInt("type"));
        data.setPlayType(rs.getInt("playType"));
        data.setGuessNum(rs.getInt("guessNum"));
        data.setStartTime(rs.getLong("startTime"));
        data.setEndTime(rs.getLong("endTime"));
        data.setEndWaitResultTime(rs.getLong("endWaitResultTime"));
        data.setEndShowTime(rs.getLong("endShowTime"));
        data.setRankResult(rs.getString("rankResult") == null ? "" : rs.getString("rankResult"));
        //json转map
        if (!StringUtils.isNullOrEmpty(rs.getString("reward"))) {
            List<CommonGuessRewardItem> reward = JSONObject.parseArray(rs.getString("reward"), CommonGuessRewardItem.class);
            data.setReward(reward);
        }
        return data;
    }
}
